package com.watson.thread.concurrent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;

/**
 * @program: mytest
 * @description: concurrent test
 * @author: zhangpeng348@jd.com
 * @date: 2018-12-18 19:42
 **/
@SpringBootApplication
@EnableScheduling
public class ConcurrentTest extends Object{

    @Autowired
    LockTest lockTest;

    @Autowired
    LockTest lockTest1;

    @Resource
    LockTest lockTest2;

//    @Value("#{com.watson.mytest.thread.LockTest}")
//    LockTest lockTest1;

    public ConcurrentTest() {
//        lockTest = new LockTest();
    }

    public static void main(String[] args) {
        SpringApplication.run(ConcurrentTest.class, args);
        final CountDownLatch latch = new CountDownLatch(3);
        ApplicationContext applicationContext;
        BeanDefinitionParserDelegate beanDefinitionParserDelegate;
        BeanDefinition beanDefinition;
        AbstractXmlApplicationContext abstractXmlApplicationContext;
        HashMap hashMap;
/*
        new Thread() {
            @Override
            public void run() {
                try {
                    System.out.println("thread id: " + Thread.currentThread().getId() + " is running");
                    Thread.sleep(4000);
                    System.out.println("thread id: " + Thread.currentThread().getId() + " have completed");
                    latch.countDown();
//                    barrier.await();

                    System.out.println("thread id: " + Thread.currentThread().getId() + " all have completed");
                } catch (InterruptedException e) {
                    latch.countDown();
                    e.printStackTrace();
                }
            }
        }.start();


        new Thread() {
            @Override
            public void run() {
                try {
                    System.out.println("thread id: " + Thread.currentThread().getId() + " is running");
                    Thread.sleep(1000);
                    System.out.println("thread id: " + Thread.currentThread().getId() + " have completed ");
                    latch.countDown();
//                    barrier.await();
                    System.out.println("thread id: " + Thread.currentThread().getId() + " all have completed");
                } catch (InterruptedException e) {
                    latch.countDown();
                    e.printStackTrace();
                }
            }
        }.start();
        */

        try {
            System.out.println("wait thread to be completed");
            latch.await();
            System.out.println("thread has completed");
        }catch (Exception e) {

        }
    }

    @Scheduled(fixedDelay = 1000)
    public void schedule() {
        System.out.println(lockTest);
        lockTest.test();
//        System.out.println(lockTest1);
    }

    @Scheduled(fixedDelay = 1000)
    public void schedule1() {
        System.out.println(lockTest1);
        lockTest1.test();
//        System.out.println(lockTest1);
    }
}
